/*
 *  (C) Copyright Gemalto, unpublished work, created 2012.
 *  This computer program includes confidential, proprietary information
 *  and is a trade secret of Gemalto. All use, disclosure, or reproduction
 *  is prohibited unless authorized in writing by an officer of Gemalto.
 *  All Rights Reserved.
 *  
 *  Written by: psturc
 */

package com.gemalto.Kryptik.Algo;

import com.gemalto.Kryptik.Util.Hex;
import com.gemalto.Kryptik.Util.Util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;


public class HashAlgo extends AbstractAlgo implements ISignature {


    private String hashName;
    private MessageDigest digest;

    public HashAlgo(String algoName, String shortName, String hashName) { //throws AlgoException {

		this.algoName  = algoName;
		this.shortName = shortName;
		this.hashName= hashName;

		try {
			digest = MessageDigest.getInstance(hashName);
		} catch ( NoSuchAlgorithmException e) {
			//throw new AlgoException(e.getMessage());
			System.err.println(e.getMessage());
        }
//		} catch (NoSuchProviderException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//        }
      }

    public String doSignature(String plainText) throws AlgoException {
          String t = Util.omitSpaces(plainText);

	    try {
		digest.update(Hex.fromString(t));
		String sign = Hex.toString(digest.digest());
                return sign;
	    } catch (Exception e){
	    	throw new AlgoException(e.getMessage());
			//System.err.println(e);
			//return null;
	    }
    }


}
